package com.salton123.infopanel

import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.view.Gravity
import com.lzf.easyfloat.EasyFloat
import com.lzf.easyfloat.enums.ShowPattern
import com.lzf.easyfloat.enums.SidePattern
import com.lzf.easyfloat.interfaces.OnInvokeView
import com.salton123.infopanel.entity.Info
import com.salton123.qa.QualityAssistant
import com.salton123.qa.R
import com.salton123.qa.kit.ToolKitsDialog
import com.salton123.util.ScreenUtils
import java.lang.ref.WeakReference

/**
 * Time:2021/7/16 10:55
 * Author:wujinsheng1
 * Description:
 */
object InfoDispatcher : Application.ActivityLifecycleCallbacks {
    var isInstalled = false
    private var infoView: WeakReference<UsageInfoView>? = null
    private var currentAtyRef: WeakReference<Activity>? = null

    private var mX = ScreenUtils.getScreenWidth() / 2
    private var mY = ScreenUtils.dp2px(50f)
    private val infoPoller by lazy {
        object : InfoPoller() {
            override fun postInfo(info: Info) {
                infoView?.get()?.updateInfo(info)
            }
        }
    }

    fun install() {
        isInstalled = true
        infoPoller.start()
        addView()
        QualityAssistant.application.registerActivityLifecycleCallbacks(this)
    }

    fun uninstall() {
        isInstalled = false
        infoPoller.stop()
        removeView()
        QualityAssistant.application.unregisterActivityLifecycleCallbacks(this)
    }

    private fun removeView() {
        currentAtyRef?.get()?.let {
            EasyFloat.dismiss("${it.hashCode()}")
        }
    }

    private fun addView() {
        currentAtyRef?.get()?.apply {
            EasyFloat.getFloatView("${this.hashCode()}")?.let {//浮窗存在，更新位置
                if (mX != 0 && mY != 0) {
                    EasyFloat.updateFloat("${this.hashCode()}", mX, mY)
                }
            } ?: kotlin.run {   //浮窗不存在，添加浮窗
                EasyFloat.with(this)
                    .setTag("${this.hashCode()}")
                    .setShowPattern(ShowPattern.CURRENT_ACTIVITY)
                    .setSidePattern(SidePattern.DEFAULT)
                    .setImmersionStatusBar(true)
                    .setAnimator(null)
                    .setGravity(Gravity.NO_GRAVITY, mX, mY)
                    .setLayout(R.layout.view_item_simple_info, OnInvokeView { view ->
                        val target = view.findViewById<UsageInfoView>(R.id.salton_id_info_panel)
                        target.setOnClickListener { ToolKitsDialog(this).show() }
                        infoView = WeakReference(target)
                    }).registerCallback {
                        dragEnd {
                            var position = IntArray(2)
                            it.getLocationOnScreen(position)
                            mX = position[0]
                            mY = position[1]
                        }
                    }.show()
            }
        }
    }

    override fun onActivityPaused(activity: Activity) {
    }

    override fun onActivityStarted(activity: Activity) {
    }

    override fun onActivityDestroyed(activity: Activity) {
    }

    override fun onActivitySaveInstanceState(activity: Activity, bundle: Bundle) {
    }

    override fun onActivityStopped(activity: Activity) {
//        EasyFloat.getFloatView("${activity.hashCode()}")?.let {//浮窗存在，更新位置
//            mX =
//        }
    }

    override fun onActivityCreated(activity: Activity, bundle: Bundle?) {
    }

    override fun onActivityResumed(activity: Activity) {
        currentAtyRef = WeakReference(activity)
        if (isInstalled && activity !is IInterceptView) {
            addView()
        }
    }
}